<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <el-input v-model="form.sea_inventory_count_order_no" placeholder="盘点单" class="w-50"></el-input>
        <el-input v-model="form.sku" placeholder="SKU" class="w-50"></el-input>
        <VGroup>
          <el-select style="max-width: 120px" v-model="form.personRange.type" placeholder="请选择">
            <el-option v-for="item in personOptions" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
          <select-personnel v-model="form.personRange.value"></select-personnel>
        </VGroup>
        <el-select class="selectClass w-30" v-model="form.order_status" placeholder="状态">
          <el-option v-for="item in options['overseasmanage.SeaInventoryCountOrder']?.[0].seaInventoryCountOrderStatus"
            :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
        <el-select class="selectClass w-30" v-model="form.verify_status" placeholder="审核状态">
          <el-option v-for="item in options['overseasmanage.SeaInventoryCountOrder']?.[0].seaInventoryCountOrderVerify"
            :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 150px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="open('add')"><svg-icon class="text-white mr-1" icon-class="add"/>新增</el-button>
        <el-button @click="handleAudit()">审核</el-button>
        <el-button @click="handleCancel()">作废</el-button>
        <el-button class="ml-auto" @click="handleExport"><svg-icon icon-class="import" />导出</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" :wareDataList="wareDataList" @query="query"></ComModal>
    <derived-center ref="DerivedCenterRef"></derived-center>

  </VPage>
</template>
<script setup name="OverseasWarehouseInventoryVerification" lang="jsx">
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { querySeaInventoryList, verifySeaInventory, cancelSeaInventory } from './api'
import ComModal from './components/ComModal.vue'

const timeOption = [
  { label: "创建时间", value: "create_time" },
  { label: "审核时间", value: "op_time" },
]

const personOptions = [
  { label: "创建人", value: "create_user_id" },
  { label: "审核人", value: "verify_user_id" },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      personRange: { type: 'create_user_id', value: [], range: true },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    { type: 'checkbox', width: "40" },
    {
      type: "seq", title: '序号', width: 80
    },
    {
      field: 'sea_inventory_count_order_no', title: '盘点单', width: 200, slots: {
        default: ({ row }) => {
          return <div class="base-text-color" onClick={() => open('view', row.id)}>{row.sea_inventory_count_order_no}</div>
        }
      }

    },
    {
      field: 'order_status_cn', title: '盘点状态', width: 80
    },
    {
      field: 'sku_count', title: 'SKU数量', width: 120
    },
    {
      field: 'sku_num', title: 'SKU件数', width: 120
    },
    {
      field: 'stock_true_rate', title: '库存准确率(库存)', width: 160
    },
    {
      field: 'stock_amount_true_rate', title: '库存准确率(金额)', width: 160
    },
    {
      field: 'create_user', title: '盘点人', width: 120
    },
    {
      field: 'create_user', title: '创建人', width: 120
    },
    {
      field: 'create_time', title: '创建时间', width: 180
    },
    {
      field: 'verify_status_cn', title: '审核状态', width: 80
    },
    {
      field: 'verify_user', title: '审核人', width: 120
    },
    {
      field: 'verify_time', title: '审核时间', width: 180
    },
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await querySeaInventoryList({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: []
})
const { options } = toRefs(data)

// 获取枚举类
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "overseasmanage.SeaInventoryCountOrder"
    ]
  }).then(res => {
    options.value = res.data
  })
}
provide('options', options)
fetchOption()

const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}

// 审核
const handleAudit = () => {
  const records = getSelectRow().map(i => {
    return i.id
  })
  if (records.length == 0) {
    ElMessage.error('请先选择行')
    return
  }
  ElMessageBox.confirm('是否审核当前勾选项?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    verifySeaInventory({ ids: records })
      .then((res) => {
        query()
        ElMessage.success(res.message)
      })
  })
}

// 取消
const handleCancel = (id) => {
  const records = getSelectRow().map(i => {
    return i.id
  })
  if (!id) {
    if (records.length == 0) {
      ElMessage.error('请先选择行')
      return
    }
  }
  const ids = id ? [id] : records
  ElMessageBox.confirm('是否作废当前勾选项?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    cancelSeaInventory({ ids })
      .then((res) => {
        query()
        ElMessage.success(res.message)
      })
  })
}

// 导出
const DerivedCenterRef = ref()
const handleExport = (val) => {
  let condition = tableRef.value.getQueryForm()
  DerivedCenterRef.value.open({
    id: 38,
    tag_name: 'wms',
    condition
  })
}
//获取已选中的行
const getSelectRow = () => {
  return tableRef?.value.$table.getCheckboxRecords()
}

</script>